<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends REST_Controller {
    protected $rules = array(
        "setCompanyInfo" => array(
            array(
                'field' => 'CompanyName',
                'label' => '企业名称',
                'rules' => 'trim'
            ),
            array(
                'field' => 'CompanyEmployeeNum',
                'label' => '员工人数',
                'rules' => 'trim'
            ),
            array(
                'field' => 'Acreage',
                'label' => '工厂面积',
                'rules' => 'trim'
            ),
            array(
                'field' => 'SalesVolume',
                'label' => '年营业额',
                'rules' => 'trim'
            ),
            array(
                'field' => 'Province',
                'label' => '省',
                'rules' => 'trim'
            ),
            array(
                'field' => 'City',
                'label' => '市',
                'rules' => 'trim'
            ),
            array(
                'field' => 'Area',
                'label' => '区',
                'rules' => 'trim'
            ),
            array(
                'field' => 'Detail',
                'label' => '详细地址',
                'rules' => 'trim'
            ),
            array(
                'field' => 'Introduce',
                'label' => '企业介绍',
                'rules' => 'trim'
            ),
            array(
                'field' => 'photo',
                'label' => '企业logo',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'photo1',
                'label' => '营业执照',
                'rules' => 'trim|numeric'
            ),
        ),
        'setActionInfo'=>array(
            array(
                'field' => 'Title',
                'label' => '分类名称',
                'rules' => 'trim|required'
            ),
        ),
        'actionInfo'=>array(
            array(
                'field' => 'Id',
                'label' => '分类id',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'Title',
                'label' => '分类名称',
                'rules' => 'trim|required'
            ),
        ),
        'addUser'=>array(
            array(
                'field' => 'ActionId',
                'label' => '分类id',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'Uname',
                'label' => '姓名',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Phone',
                'label' => '手机号码',
                'rules' => 'trim|required'
            ),
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
            ),
            array(
                'field' => 'Email',
                'label' => '邮箱',
                'rules' => 'trim|required|is_mail'
            ),
        ),
        'setSubsidiaryInfo'=>array(
            array(
                'field' => 'Id',
                'label' => '子账户id',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'ActionId',
                'label' => '分类id',
                'rules' => 'trim|numeric|required'
            ),
            array(
                'field' => 'Uname',
                'label' => '姓名',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Phone',
                'label' => '手机号码',
                'rules' => 'trim|is_mobile|required'
            ),
            array(
                'field'   => 'Password',
                'label'   => '密码',
                'rules'   => 'trim|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
            ),
            array(
                'field' => 'Email',
                'label' => '邮箱',
                'rules' => 'trim|is_mail|required'
            ),
        ),
        'setMessageSet'=>array(
            //share_instation,share_email,share_note,data_instation,data_email,data_note,system_instation,system_email,system_note,adv_instation,adv_email,adv_note
            array(
                'field' => 'ShareInstation',
                'label' => '分享消息站内信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'ShareEmail',
                'label' => '分享消息邮箱设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'ShareNote',
                'label' => '分享消息短信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'DataInstation',
                'label' => '数据统计站内信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'DataEmail',
                'label' => '数据统计邮箱设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'DataNote',
                'label' => '数据统计短信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'SystemInstation',
                'label' => '系统信息站内信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'SystemEmail',
                'label' => '系统信息邮箱设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'SystemNote',
                'label' => '系统信息短信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'AdvInstation',
                'label' => '广告推广站内信设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'AdvEmail',
                'label' => '广告推广邮箱设置',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'AdvNote',
                'label' => '广告推广短信设置',
                'rules' => 'trim|numeric'
            ),
        ),
        'setRetroaction'=>array(
            array(
                'field' => 'Content',
                'label' => '内容',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Photo',
                'label' => '图片',
                'rules' => 'trim'
            ),
        ),
        'addFolder'=>array(
            array(
                'field' => 'Title',
                'label' => '标题',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Fid',
                'label' => '父类id',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'Cid',
                'label' => '文件夹类型',
                'rules' => 'trim|numeric|required'
            ),
        ),
        "addOmnibusInfo"=>array(
            array(
                'field' => 'Ctype',
                'label' => '精选集id',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'Type',
                'label' => '内容类型',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'ProId',
                'label' => '内容id',
                'rules' => 'trim|required|numeric'
            ),
            array(
                'field' => 'ProName',
                'label' => '内容标题',
                'rules' => 'trim|required'
            ),
        ),
        "addDocument"=>array(
            array(
                'field' => 'Ctype',
                'label' => '文件夹id',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'Title',
                'label' => '文件名',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'Photo',
                'label' => '文件',
                'rules' => 'trim|required|numeric'
            ),
        ),
    );

    private $rdata = array();
    protected $compress_width_height = COMPRESS_WIDTH_HEIGHT;//图片压缩宽高
    protected $compress_size = COMPRESS_SIZE;//图片压缩大小
    protected $img_format = IMG_FORMAT;//上传图片格式
    protected $img_size = PCIMG_SIZE;//上传图片大小
    function __construct(){
        session_start();
        parent::__construct();

        $this->rdata['Secure'] = false;
        $this->returnInfo = lang('returnInfo');
        $this->rdata['ReturnCode'] = '404';
        $this->rdata['ReturnInfo'] = '获取失败';
        $this->rdata['Content2'] = array();
        $this->rdata['Content'] = array();
        $this->load->helpers('uisite_helper');
    }
    /*
     * 获取企业信息
     */
    public function getCompanyInfo_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $res=$this->common->onceInfo(array('user_id'=>$userid),'company_name,company_employee_num,acreage,sales_volume,province,city,area,detail,introduce,vr_url,photo,photo1,status,vr_status','user_company');
        if(!empty($res)){
            if(!empty($res['photo'])){
                $res['photo']=upload_file(tag_photo($res['photo']));
            }
            if(!empty($res['photo1'])){
                $res['photo1']=upload_file(tag_photo($res['photo1']));
            }
        }
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '信息获取成功';
        $this->rdata['Content2'] = $res;
        $this->rdata['Content'] = $res;
        $this->response($this->rdata);
    }
    /*
     * 设置企业信息
     */
    public function setCompanyInfo_patch(){
        $_POST=$this->patch();
        $data=$_POST;
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['setCompanyInfo']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法设置企业信息';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('user_id'=>$userid),'user_company');
        if($count==0){//添加
            $company_name=isset($data['CompanyName'])?$data['CompanyName']:'';
            $company_employee_num=isset($data['CompanyEmployeeNum'])?$data['CompanyEmployeeNum']:'';
            $acreage=isset($data['Acreage'])?$data['Acreage']:'';
            $sales_volume=isset($data['SalesVolume'])?$data['SalesVolume']:'';
            $province=isset($data['Province'])?$data['Province']:'';
            $city=isset($data['City'])?$data['City']:'';
            $area=isset($data['Area'])?$data['Area']:'';
            $detail=isset($data['Detail'])?$data['Detail']:'';
            $introduce=isset($data['Introduce'])?$data['Introduce']:'';
            $photo=isset($data['photo'])?$data['photo']:'';
            $photo1=isset($data['photo1'])?$data['photo1']:'';
            if(empty($company_name)){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '企业名称必填';
                $this->response($this->rdata);
            }
            $insert=array(
                'company_name'=>$company_name,
                'company_employee_num'=>$company_employee_num,
                'acreage'=>$acreage,
                'sales_volume'=>$sales_volume,
                'province'=>$province,
                'city'=>$city,
                'area'=>$area,
                'detail'=>$detail,
                'introduce'=>$introduce,
                'photo'=>$photo,
                'status'=>1,
                'photo1'=>$photo1,
                'user_id'=>$userid
            );
            $res=$this->common->insertInfo('user_company',$insert);
            if($res){
                $message=$this->common->onceInfo(array('user_id'=>$userid),'company_name,company_employee_num,acreage,sales_volume,province,city,area,detail,introduce,vr_url,photo,photo1,status,vr_status','user_company');
                if(!empty($message)){
                    if(!empty($message['photo'])){
                        $message['photo']=upload_file(tag_photo($message['photo']));
                    }
                    if(!empty($message['photo1'])){
                        $message['photo1']=upload_file(tag_photo($message['photo1']));
                    }
                }
                $this->rdata['ReturnCode']   = '200';
                $this->rdata['ReturnInfo'] = '操作成功';
                $this->rdata['Content2'] = $message;
                $this->rdata['Content'] = $message;
                $this->response($this->rdata);
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '操作失败';
                $this->response($this->rdata);
            }
        }else{//更新
            $res=$this->common->onceInfo(array('user_id'=>$userid),'company_name,company_employee_num,acreage,sales_volume,province,city,area,detail,introduce,vr_url,photo,photo1,status,vr_status','user_company');
            if($res['status']==2){
                $company_name=$res['company_name'];
                if(!empty($res['photo1'])){
                    $photo1=$res['photo1'];
                }else{
                    $photo1=isset($data['photo1'])?$data['photo1']:$res['photo1'];
                }
            }else{
                $company_name=isset($data['CompanyName'])?$data['CompanyName']:$res['company_name'];
                $photo1=isset($data['photo1'])?$data['photo1']:$res['photo1'];
            }
            $company_employee_num=isset($data['CompanyEmployeeNum'])?$data['CompanyEmployeeNum']:$res['company_employee_num'];
            $acreage=isset($data['Acreage'])?$data['Acreage']:$res['acreage'];
            $sales_volume=isset($data['SalesVolume'])?$data['SalesVolume']:$res['sales_volume'];
            $province=isset($data['Province'])?$data['Province']:$res['province'];
            $city=isset($data['City'])?$data['City']:$res['city'];
            $area=isset($data['Area'])?$data['Area']:$res['area'];
            $detail=isset($data['Detail'])?$data['Detail']:$res['detail'];
            $introduce=isset($data['Introduce'])?$data['Introduce']:$res['introduce'];
            $photo=isset($data['photo'])?$data['photo']:$res['photo'];
            $update=array(
                'company_name'=>$company_name,
                'company_employee_num'=>$company_employee_num,
                'acreage'=>$acreage,
                'sales_volume'=>$sales_volume,
                'province'=>$province,
                'city'=>$city,
                'area'=>$area,
                'detail'=>$detail,
                'introduce'=>$introduce,
                'photo'=>$photo,
                'photo1'=>$photo1
            );
            if($res['status']!=2){
                $update=array_merge($update,array('status'=>1));
            }
            $v=$this->common->updateInfo('user_company',$update,array('user_id'=>$userid));
            if($v){
                $message=$this->common->onceInfo(array('user_id'=>$userid),'company_name,company_employee_num,acreage,sales_volume,province,city,area,detail,introduce,vr_url,photo,photo1,status,vr_status','user_company');
                if(!empty($message)){
                    if(!empty($message['photo'])){
                        $message['photo']=upload_file(tag_photo($message['photo']));
                    }
                    if(!empty($message['photo1'])){
                        $message['photo1']=upload_file(tag_photo($message['photo1']));
                    }
                }
                $this->rdata['ReturnCode']   = '200';
                $this->rdata['ReturnInfo'] = '操作成功';
                $this->rdata['Content2'] = $message;
                $this->rdata['Content'] = $message;
                $this->response($this->rdata);
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '操作失败';
                $this->response($this->rdata);
            }
        }
    }
    /*
     * 上传图片
     */
    public function returnPath_post(){
        $this->load->model('common_model','common','default');
        $now_time=time();
        $date = date('Y/m/d', $now_time);
        if ($_FILES) {

            $uploadPath = UPLOAD_PATH  . $date;
            if (!file_exists($uploadPath)) {
                $res = mkdir($uploadPath,0777,true);
                chmod($uploadPath,0777);
            }

            $config['upload_path'] = $uploadPath. '/';
            $config['encrypt_name'] = TRUE;
            /*$config['max_width']  = '1024';
             $config['max_height']  = '768';*/
            $config['allowed_types'] = $this->img_format;
            $config['max_size'] = $this->img_size;
            $this->load->library('upload');
            $this->upload->initialize($config);

            //计算图片上传上限
            $photoCount=0;

            foreach ($_FILES as $k => $val) {
                if (strpos($k, 'hoto')) {
                    if (!empty($val['name'])) {
                        $photoCount++;
                    }
                }
                if($val['size']>$this->img_size){
                    $this->rdata['ReturnCode']   = '404';
                    $this->rdata['ReturnInfo'] = '请将图片压缩至10M以内再上传';
                    $this->response($this->rdata);
                }
            }

            if(!$photoCount){
                $this->rdata['ReturnCode']   = '404';
                $this->rdata['ReturnInfo'] = '请上传图片';
                $this->response($this->rdata);
            }
            if($photoCount>20){
                $this->rdata['ReturnCode']   = '404';
                $this->rdata['ReturnInfo'] = '最多上传20张图片';
                $this->response($this->rdata);
            }

            //执行图片上传
            foreach ($_FILES as $k => $val) {
                $field_name = $k;
                if (strpos($field_name, 'hoto')) {
                    if ($photoCount <= 5) {//上传图片
                        if (!empty($val['name'])) {
                            if ($this->upload->do_upload($field_name)) {
                                $temp = $this->upload->data();
                                $photo[] = $temp;
                                $photoCount--;

                            }else{
                                if(isset($photo)&&!empty($photo)){
                                    foreach ($photo as $key => $value) {
                                        @unlink($value['full_path']);
                                    }
                                }
                                $this->rdata['ReturnCode']   = '404';
                                $this->rdata['ReturnInfo'] = trim(strip_tags($this->upload->display_errors()),'.');
                                $this->response($this->rdata);
                            }
                        }
                    }
                }
            }
        }else{
            $this->rdata['ReturnCode']   = '404';
            $this->rdata['ReturnInfo'] = "请上传图片";
            $this->response($this->rdata);
        }

        foreach ($photo as $k => $item) {
            $this->imgResize($date . '/' . $item['file_name']);
            $photoInsert[$k] = array(
                'name' => $item['file_name'],
                'size' => $item['file_size'] * 1024,
                'type' => $item['file_type'],
                'thumb' => $date . '/' . $item['file_name'],
                'url' => $date . '/' . $item['file_name'],
                'deleteUrl' => '?file=' . $item['file_name'] . '&dt=' . $date,
                'timeline' => now()
            );
            $this->common->imgResize(UPLOAD_PATH.$date.'/'.$item['file_name']);
            $this->common->insertInfo('upload', $photoInsert[$k]);
            $iid = $this->db->insert_id();
            $photoInsertId[$k]['id'] = $iid;
            $photoInsertId[$k]['url'] = UPLOAD_URL.$date . '/' . $item['file_name'];
        }
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = "获取成功";
        $this->rdata['Content2'] = $photoInsertId;
        $this->rdata['Content'] = $photoInsertId;
        $this->response($this->rdata);

    }
    //删除图片
    public function del_img($id){
        if(!empty($id)){
            $url=tag_photo($id);
            if(!empty($url)){
                $where['id']=$id;
                $this->common->deleteInfo('upload',$where);
                @unlink(UPLOAD_PATH.$url);
            }
        }
    }

    /*
     * 获取分组列表信息
     */
    public  function getActionInfo_get(){
        $this->load->model('common_model','common','default');
        $data=$this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $per=isset($data['Limit'])?$data['Limit']:10;
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法获取分组信息';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('user_id'=>$userid),'user_action');
        $list=$this->common->pageinfotoorder(array('user_id'=>$userid),'id,title',($page-1)*$per,$per,'id desc','user_action');
        $vdata['list']=$list;
        $vdata['count']=$count;
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '信息获取成功';
        $this->rdata['Content2'] = $vdata;
        $this->rdata['Content'] = $vdata;
        $this->response($this->rdata);
    }
    /*
     * 添加账户分类
     */
    public function setActionInfo_post(){
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['setActionInfo']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法添加分类信息';
            $this->response($this->rdata);
        }
        $data=$this->post();
        $count=$this->common->countLog(array('user_id'=>$userid,'title'=>$data['Title']),'user_action');
        if($count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该分类已存在';
            $this->response($this->rdata);
        }
        $insert=array(
            'title'=>$data['Title'],
            'user_id'=>$userid,
            'timeline'=>now()
        );
        $res=$this->common->insertInfo('user_action',$insert);
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '添加成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '添加失败';
            $this->response($this->rdata);
        }
    }
    /*
     * 编辑账户分类
     */
    public function actionInfo_patch(){
        $_POST=$this->patch();
        $data=$_POST;
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['actionInfo']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法编辑分类信息';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('id'=>$data['Id'],'user_id'=>$userid),'user_action');
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该分类不存在';
            $this->response($this->rdata);
        }
        $update=array(
            'title'=>$data['Title']
        );
        $res=$this->common->updateInfo('user_action',$update,array('id'=>$data['Id'],'user_id'=>$userid));
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '修改成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '修改失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 删除账户分类
     */
    public function delActionInfo_delete(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法编辑分类信息';
            $this->response($this->rdata);
        }
        $data=$this->delete();
        $count=$this->common->countLog(array('id'=>$data['Id'],'user_id'=>$userid),'user_action');
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该分类不存在';
            $this->response($this->rdata);
        }
        $res=$this->common->deleteInfo('user_action',array('id'=>$data['Id']));
        if($res){
            $this->common->updateInfo('user',array('action_id'=>""),array('action_id'=>$data['Id']));
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '删除成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '删除失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 添加子账户
     */
    public function  addUser_post(){
        $this->load->model('common_model','common','default');
        $this->load->model('user_model','user','default');
        $this->form_validation->set_rules($this->rules['addUser']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法操作';
            $this->response($this->rdata);
        }
        $data=$this->post();
        $count=$this->common->countLog(array('id'=>$data['ActionId'],'user_id'=>$userid),'user_action');
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该分类不存在';
            $this->response($this->rdata);
        }
        $ation=$this->common->onceInfo(array('id'=>$data['ActionId'],'user_id'=>$userid),'title','user_action');
        $user_count=$this->common->countLog(array('phone'=>$data['Phone'],'phone_valid'=>1,'is_delect'=>0),'user');
        if($user_count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该手机号已注册';
            $this->response($this->rdata);
        }
        $email_count=$this->common->countLog(array('email'=>$data['Email'],'is_delect'=>0),'user');
        if($email_count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该邮箱已使用';
            $this->response($this->rdata);
        }
        $insert=array(
            'phone'=>$data['Phone'],
            'phone_valid'=>1,
            'type'=>0,
            'email'=>$data['Email'],
            'password'=>passwd($data['Password']),
            'uname'=>$data['Uname'],
            'timeline'=>now(),
            'action_id'=>$data['ActionId'],
            'action_name'=>$ation['title'],
            'fid'=>$userid
        );
        $res=$this->common->insertInfo('user',$insert);
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '添加成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '添加失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 获取子账户列表信息
     */
    public function getSubsidiaryInfo_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '无法获取子账户信息';
            $this->response($this->rdata);
        }
        $data=$this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $per=isset($data['Limit'])?$data['Limit']:10;
        $action_id=isset($data['ActionId'])?$data['ActionId']:"";
        $unameOrPhone=isset($data['UnameOrPhone'])?$data['UnameOrPhone']:"";
        //获取所有的分类
        $action=$this->common->allInfoorder(array('user_id'=>$userid),'id,title','id desc','user_action');
        $vdata['action']=$action;
        $where=array('fid'=>$userid,'is_delect'=>0);
        if(!empty($action_id)){
            $where=array_merge($where,array('action_id'=>$action_id));
        }
        if(!empty($unameOrPhone)){
            $where_q = array(
                'like phone'=> array('phone',$unameOrPhone),
                'or_like uname'=> array('uname',$unameOrPhone)
            );
            $where=array_merge($where,$where_q);
        }
        $count=$this->common->countLog($where,'user');
        $list=$this->common->pageinfotoorder($where,'id,uname,action_name,phone',($page-1)*$per,$per,'id desc','user');
        $vdata['count']=$count;
        $vdata['list']=$list;
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '信息获取成功';
        $this->rdata['Content2'] = $vdata;
        $this->rdata['Content'] = $vdata;
        $this->response($this->rdata);
    }

    /*
     * 编辑子账户信息
     */
    public function setSubsidiaryInfo_patch(){
        $_POST=$this->patch();
        $data=$_POST;
        $this->load->model('common_model','common','default');
        $this->form_validation->set_rules($this->rules['setSubsidiaryInfo']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法编辑信息';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('id'=>$data['Id'],'type'=>0,'fid'=>$userid,'is_delect'=>0),'user');
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该账户不存在';
            $this->response($this->rdata);
        }
        $res=$this->common->onceInfo(array('id'=>$data['Id'],'type'=>0,'fid'=>$userid,'is_delect'=>0),'uname,phone,password,email,action_id','user');
        $action_id=isset($data['ActionId'])?$data['ActionId']:$res['action_id'];
        $uname=isset($data['Uname'])?$data['Uname']:$res['uname'];
        $phone=isset($data['Phone'])?$data['Phone']:$res['phone'];
        $password=isset($data['Password'])?$data['Password']:"";
        $email=isset($data['Email'])?$data['Email']:$res['email'];
        $ation=$this->common->onceInfo(array('id'=>$action_id,'user_id'=>$userid),'title','user_action');
        if($phone!=$res['phone']){
            $user_count=$this->common->countLog(array('phone'=>$phone,'phone_valid'=>1),'user');
            if($user_count>0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该手机号已注册';
                $this->response($this->rdata);
            }
        }
        if($email!=$res['email']){
            $email_count=$this->common->countLog(array('email'=>$data['Email']),'user');
            if($email_count>0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该邮箱已使用';
                $this->response($this->rdata);
            }
        }
        $update=array(
            'uname'=>$uname,
            'phone'=>$phone,
            'email'=>$email,
            'action_id'=>$action_id,
            'action_name'=>$ation['title']
        );
        if(!empty($password)){
            $update=array_merge($update,array('password'=>passwd($password)));
        }
        $res=$this->common->updateInfo('user',$update,array('id'=>$data['Id']));
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '修改成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '修改失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 删除子账户(不需要联动删除)
     */
    public function delSubsidiaryInfo_delete(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法操作';
            $this->response($this->rdata);
        }
        $data=$this->delete();
        $count=$this->common->countLog(array('id'=>$data['Id'],'type'=>0,'fid'=>$userid,'is_delect'=>0),'user');
        if($count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该账户不存在';
            $this->response($this->rdata);
        }
        $res=$this->common->updateInfo('user',array('is_delect'=>1),array('id'=>$data['Id'],'type'=>0,'fid'=>$userid));
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '删除成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '删除失败';
            $this->response($this->rdata);
        }
    }
    //消息中心
    /*
     * 获取消息设置信息
     */
    public function  getMessageSet_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $tty   = $this->em_getallheaders('PLATFORM');
        if($tty==2){
            $res=$this->common->onceInfo(array('user_id'=>$userid),'share_instation,share_email,share_note,data_instation,data_email,data_note,system_instation,system_email,system_note,adv_instation,adv_email,adv_note','message_set');
            $vdata['info']=$res;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = 'App无法获取消息设置信息';
            $this->response($this->rdata);
        }
    }

    /*
     * 获取消息中心的系统消息和推广消息
     * type:1-系统消息，2-广告推广信息
     */
    public function getSystemAndAdvMessage_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $data=$this->get();
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $per=isset($data['Limit'])?$data['Limit']:10;
        if($data['Type']==1){
            $list=$this->common->pageinfotoorder(array('cid'=>6,'audit'=>1),'title,content,timeline,id',($page-1)*$per,$per,'sort_id desc','message');
            if(!empty($list)){
                foreach($list as $k=>$v){
                    $list[$k]['timelin']=date('Y-m-d H:i:s',$v['timeline']);
                }
            }
            $vdata['list']=$list;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }elseif($data['Type']==2){
            $list=$this->common->pageinfotoorder(array('cid'=>7,'audit'=>1),'title,content,timeline,id',($page-1)*$per,$per,'sort_id desc','message');
            if(!empty($list)){
                foreach($list as $k=>$v){
                    $list[$k]['timelin']=date('Y-m-d H:i:s',$v['timeline']);
                }
            }
            $vdata['list']=$list;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
    }


    /*
     * 消息中心的设置
     */
    public function setMessageSet_post(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $this->form_validation->set_rules($this->rules['setMessageSet']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $data=$this->post();
        $count=$this->common->countLog(array('user_id'=>$userid),'message_set');
        if($count==0){
            $share_instation=isset($data['ShareInstation'])?$data['ShareInstation']:0;
            $share_email=isset($data['ShareEmail'])?$data['ShareEmail']:0;
            $share_note=isset($data['ShareNote'])?$data['ShareNote']:0;
            $data_instation=isset($data['DataInstation'])?$data['DataInstation']:0;
            $data_email=isset($data['DataEmail'])?$data['DataEmail']:0;
            $data_note=isset($data['DataNote'])?$data['DataNote']:0;
            $system_instation=isset($data['SystemInstation'])?$data['SystemInstation']:0;
            $system_email=isset($data['SystemEmail'])?$data['SystemEmail']:0;
            $system_note=isset($data['SystemNote'])?$data['SystemNote']:0;
            $adv_instation=isset($data['AdvInstation'])?$data['AdvInstation']:0;
            $adv_email=isset($data['AdvEmail'])?$data['AdvEmail']:0;
            $adv_note=isset($data['AdvNote'])?$data['AdvNote']:0;
            $insert=array(
                'share_instation'=>$share_instation,
                'share_email'=>$share_email,
                'share_note'=>$share_note,
                'data_instation'=>$data_instation,
                'data_email'=>$data_email,
                'data_note'=>$data_note,
                'system_instation'=>$system_instation,
                'system_email'=>$system_email,
                'system_note'=>$system_note,
                'adv_instation'=>$adv_instation,
                'adv_email'=>$adv_email,
                'adv_note'=>$adv_note,
                'user_id'=>$userid,
                'timeline'=>now()
            );
            $res=$this->common->insertInfo('message_set',$insert);
            if($res){
                $this->rdata['ReturnCode']   = '200';
                $this->rdata['ReturnInfo'] = '添加成功';
                $this->response($this->rdata);
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '添加失败';
                $this->response($this->rdata);
            }
        }else{
            $res=$this->common->onceInfo(array('user_id'=>$userid),'share_instation,share_email,share_note,data_instation,data_email,data_note,system_instation,system_email,system_note,adv_instation,adv_email,adv_note','message_set');
            $share_instation=isset($data['ShareInstation'])?$data['ShareInstation']:$res['share_instation'];
            $share_email=isset($data['ShareEmail'])?$data['ShareEmail']:$res['share_email'];
            $share_note=isset($data['ShareNote'])?$data['ShareNote']:$res['share_note'];
            $data_instation=isset($data['DataInstation'])?$data['DataInstation']:$res['data_instation'];
            $data_email=isset($data['DataEmail'])?$data['DataEmail']:$res['data_email'];
            $data_note=isset($data['DataNote'])?$data['DataNote']:$res['data_note'];
            $system_instation=isset($data['SystemInstation'])?$data['SystemInstation']:$res['system_instation'];
            $system_email=isset($data['SystemEmail'])?$data['SystemEmail']:$res['system_email'];
            $system_note=isset($data['SystemNote'])?$data['SystemNote']:$res['system_note'];
            $adv_instation=isset($data['AdvInstation'])?$data['AdvInstation']:$res['adv_instation'];
            $adv_email=isset($data['AdvEmail'])?$data['AdvEmail']:$res['adv_email'];
            $adv_note=isset($data['AdvNote'])?$data['AdvNote']:$res['adv_note'];
            $update=array(
                'share_instation'=>$share_instation,
                'share_email'=>$share_email,
                'share_note'=>$share_note,
                'data_instation'=>$data_instation,
                'data_email'=>$data_email,
                'data_note'=>$data_note,
                'system_instation'=>$system_instation,
                'system_email'=>$system_email,
                'system_note'=>$system_note,
                'adv_instation'=>$adv_instation,
                'adv_email'=>$adv_email,
                'adv_note'=>$adv_note,
                'timeline'=>now()
            );
            $res=$this->common->updateInfo('message_set',$update,array('user_id'=>$userid));
            if($res){
                $this->rdata['ReturnCode']   = '200';
                $this->rdata['ReturnInfo'] = '修改成功';
                $this->response($this->rdata);
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '修改失败';
                $this->response($this->rdata);
            }
        }
    }

    /*
     * 意见反馈
     */
    public function setRetroaction_post(){
        $this->load->model('common_model','common','default');
        $this->load->helpers('funs_helper');
        $userid=$this->uinfo['id'];
        $uname=$this->uinfo['uname'];
        $phone=$this->uinfo['phone'];
        $this->form_validation->set_rules($this->rules['setRetroaction']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $data=$this->post();
        $insert=array(
            'uname'=>$uname,
            'user_id'=>$userid,
            'phone'=>$phone,
            'content'=>$data['Content'],
            'photo'=>$data['Photo'],
            'timeline'=>now()
        );
        $res=$this->common->insertInfo('retroaction',$insert);
        if($res){
            $res=$this->common->onceInfo(array('cid'=>10,'audit'=>1),'title','article');
            if($res){
                $content='<table style="padding-left: 35%"><tr><td>用户名:</td><td>'.$uname.'</td></tr><tr><td>手机号码:</td><td>'.$phone.'</td></tr><tr><td>反馈内容:</td><td>'.$data['Content'].'</td></tr></table>';
                $me=sendPHPMailer($res['title'],'意见反馈',$content);  //$this->send($res['title'],'海南智造慧科技有限公司','意见反馈 ',$content);
                if($me){
                    $this->rdata['ReturnCode']   = '200';
                    $this->rdata['ReturnInfo'] = '操作成功';
                    $this->response($this->rdata);
                }else{
                    $this->rdata['ReturnCode']   = '400';
                    $this->rdata['ReturnInfo'] = '邮件发送失败';
                    $this->response($this->rdata);
                }
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '请设置接收邮箱';
                $this->response($this->rdata);
            }
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '操作失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 申请VR工厂
     */
    public function applyVR_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $uname=$this->uinfo['uname'];
        $phone=$this->uinfo['phone'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']!=1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '子账户无法操作';
            $this->response($this->rdata);
        }
        $res=$this->common->onceInfo(array('user_id'=>$userid),'vr_status,vr_url','user_company');
        if($res['vr_status']==1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '已申请VR工厂，请等待后台审核';
            $this->response($this->rdata);
        }elseif($res['vr_status']==2){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '审核已通过';
            $this->response($this->rdata);
        }else{
            $res=$this->common->updateInfo('user_company',array('vr_status'=>1),array('user_id'=>$userid));
            if($res){
                $mes=$this->common->onceInfo(array('cid'=>9,'audit'=>1),'title','article');
                if($mes){
                    $content='用户'.$uname.'('.$phone.')申请VR工厂,请尽快在后台审核';
                    $me=sendPHPMailer($mes['title'],'VR工厂',$content);
                    //$me=$this->send($mes['title'],'海南智造慧科技有限公司','VR工厂 ',$content);
                    if($me){
                        $this->rdata['ReturnCode']   = '200';
                        $this->rdata['ReturnInfo'] = '操作成功,请等待内容审核';
                        $this->response($this->rdata);
                    }else{
                        $this->rdata['ReturnCode']   = '400';
                        $this->rdata['ReturnInfo'] = '邮件发送失败';
                        $this->response($this->rdata);
                    }
                }else{
                    $this->rdata['ReturnCode']   = '400';
                    $this->rdata['ReturnInfo'] = '请设置接收邮箱';
                    $this->response($this->rdata);
                }
            }else{
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '操作失败';
                $this->response($this->rdata);
            }
        }
    }

    /*
    * 添加文件夹
     * cid ---1产品，2文档，3精选集
     * fid---父类id
     * title--标题
     * photo--封面图
    */
    public function  addFolder_post(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $this->form_validation->set_rules($this->rules['addFolder']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $data=$this->post();
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        $fid=isset($data['Fid'])?$data['Fid']:0;
        $cid=$data['Cid'];
        $title=$data['Title'];
        $photo=isset($data['Photo'])?$data['Photo']:"";
        $tty   = $this->em_getallheaders('PLATFORM');
        $depth=0;
        $sort=$this->common->onceInfoBySort('sort_id','directory');
        if(empty($sort)){
            $sort_id=1;
        }else{
            $sort_id=(int)$sort['sort_id']+1;
        }
        if($cid==1){//产品，主账户，pc
            if($tty!=2){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = 'APP无法完成此操作';
                $this->response($this->rdata);
            }
            if($user['type']!=1){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '子账户无法完成此操作';
                $this->response($this->rdata);
            }
        }elseif($cid==2){//文档

        }elseif($cid==3){//精选集，子账户
            if($user['type']==1){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '主账户无法完成此操作';
                $this->response($this->rdata);
            }
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
        if($fid!=0){
            $count=$this->common->countLog(array('id'=>$fid,'cid'=>$cid),'directory');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '父类id不存在';
                $this->response($this->rdata);
            }
            $depth=1;
        }
        $insert=array(
            'fid'=>$fid,
            'depth'=>$depth,
            'title'=>$title,
            'cid'=>$cid,
            'user_id'=>$userid,
            'sort_id'=>$sort_id,
            'photo'=>$photo
        );
        $res=$this->common->insertInfo('directory',$insert);
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '添加成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '添加失败';
            $this->response($this->rdata);
        }
    }

    /*
     * 获取精选集文件夹
     */
    public function getOmnibusFolder_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $per=isset($data['Limit'])?$data['Limit']:10;
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']==1){//主账户
            $users=$this->common->allInfoorder(array('fid'=>$userid),'id','id desc','user');
            if(!empty($users)){
                $ids=array();
                foreach($users as $v){
                    $ids[]=$v['id'];
                }
                $list=$this->db->where_in('user_id',$ids)->where(array('cid'=>3))->select('id,title,timeline')->order_by('sort_id desc')->get('directory')->result_array();
            }else{
                $list=array();
            }
            if(!empty($list)){
                foreach($list as $k=>$item){
                    $list[$k]['timeline']=date('Y/m/d',$item['timeline']);
                }
            }
            $count=count($list);
            $list=array_slice($list,($page-1)*$per,$per);
            $vdata['list']=$list;
            $vdata['count']=$count;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }else{//子账户
            $list=$this->common->allInfoorder(array('cid'=>3,'user_id'=>$userid),'id,title,timeline','sort_id desc','directory');
            if(!empty($list)){
                foreach($list as $k=>$item){
                    $list[$k]['timeline']=date('Y/m/d',$item['timeline']);
                }
            }
            $count=count($list);
            $list=array_slice($list,($page-1)*$per,$per);
            $vdata['list']=$list;
            $vdata['count']=$count;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }

    }

    /*
     * 删除精选集
     * 精选集文件夹中存在文件就不能删除
     */
    public function delOmnibusFolder_delete(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']==1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '无操作权限';
            $this->response($this->rdata);
        }
        $data=$this->delete();
        $mes_count=$this->common->countLog(array('user_id'=>$userid,'id'=>$data['Id']),'directory');
        if($mes_count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该精选集不存在';
            $this->response($this->rdata);
        }
        //是否存在二级分类
        $res_count=$this->common->countLog(array('user_id'=>$userid,'fid'=>$data['Id']),'directory');
        if($res_count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该精选集存在文件夹，无法删除';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('user_id'=>$userid,'ctype'=>$data['Id']),'omnibus');
        if($count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该精选集有内容，无法删除';
            $this->response($this->rdata);
        }
        $v=$this->common->deleteInfo('directory',array('user_id'=>$userid,'id'=>$data['Id']));
        if($v){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '删除成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '删除失败';
            $this->response($this->rdata);
        }
    }
    /*
     * 添加精选集内容
     */
    public function addOmnibusInfo_post(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $this->form_validation->set_rules($this->rules['addOmnibusInfo']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }
        $data=$this->post();
        $mes_count=$this->common->countLog(array('user_id'=>$userid,'id'=>$data['Ctype']),'directory');
        if($mes_count==0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该精选集不存在';
            $this->response($this->rdata);
        }
        $user=$this->common->onceInfo(array('id'=>$userid,'is_delect'=>0),'type','user');
        if($user['type']==1){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '无操作权限';
            $this->response($this->rdata);
        }
        $count=$this->common->countLog(array('user_id'=>$userid,'ctype'=>$data['Ctype'],'pro_id'=>$data['ProId']),'omnibus');
        if($count>0){
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '该内容已在此精选集';
            $this->response($this->rdata);
        }
        $insert=array(
            'user_id'=>$userid,
            'ctype'=>$data['Ctype'],
            'type'=>$data['Type'],
            'pro_id'=>$data['ProId'],
            'pro_name'=>$data['ProName'],
            'timeline'=>now()
        );
        $v=$this->common->insertInfo('omnibus',$insert);
        if($v){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '添加成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '添加失败';
            $this->response($this->rdata);
        }
    }
    /*
     * 文档资料中添加文件
     * title标题
     * photo文件
     * ctype文件夹id
     */
    public function addDocument_post(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $this->form_validation->set_rules($this->rules['addDocument']);
        if ($this->form_validation->run() == FALSE) {
            // 返回失败
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = validation_errors();
            $this->response($this->rdata);
        }

        $data=$this->post();
        $ctype=isset($data['Ctype'])?$data['Ctype']:0;
        if(!empty($ctype)){
            $count=$this->common->countLog(array('id'=>$ctype,'cid'=>2),'directory');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该文件夹不存在';
                $this->response($this->rdata);
            }
        }
        $insert=array(
            'ctype'=>$ctype,
            'user_id'=>$userid,
            'timeline'=>now(),
            'title'=>$data['Title'],
            'photo'=>$data['Photo']
        );
        $res=$this->common->insertInfo('document',$insert);
        if($res){
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '添加成功';
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '添加失败';
            $this->response($this->rdata);
        }
    }
    /*
     * 查看文档资料
     * type---1文件夹，0文件
     */
    public function getDocument_get(){
        $this->load->model('common_model','common','default');
        $userid=$this->uinfo['id'];
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $per=isset($data['Limit'])?$data['Limit']:10;
        $list=$this->common->pageinfotoorder(array('user_id'=>$userid),'id,ctype,title,photo,timeline',($page-1)*$per,$per,'id desc','document');
        $count=count($list);
        $vdata['count']=$count;
        if(!empty($list)){
            $ctype=array();
            foreach($list as $v){
                if($v['ctype']!=0){
                    $ctype[]=$v['ctype'];
                }
            }
            if(!empty($ctype)){
                $ctype_list=$this->db->where_in('id',$ctype)->where(array('user_id'=>$userid,'cid'=>2))->select('id,title,photo,timeline')->order_by('sort_id desc')->get('directory')->result_array();
                $document_title=array();
                $document_photo=array();
                $document_timeline=array();
                if(!empty($ctype_list)){
                    foreach($ctype_list as $v){
                        $document_title[$v['id']]=$v['title'];
                        $document_photo[$v['id']]=$v['photo'];
                        $document_timeline[$v['id']]=$v['timeline'];
                    }
                }
            }
            foreach($list as $k=>$v){
                if($v['ctype']!=0){
                    $list[$k]['type']=1;
                    if(isset($document_title[$v['ctype']])){
                        $list[$k]['title']=$document_title[$v['ctype']];
                    }else{
                        $list[$k]['title']=$v['title'];
                    }
                    if(isset($document_photo[$v['ctype']])){
                        $list[$k]['photo']=$document_photo[$v['ctype']];
                    }else{
                        $list[$k]['photo']="";
                    }
                    if(isset($document_timeline[$v['timeline']])){
                        $list[$k]['timeline']=$document_timeline[$v['timeline']];
                    }else{
                        $list[$k]['timeline']=$v['timeline'];
                    }
                }else{
                    $list[$k]['type']=0;
                }
            }
            foreach($list as $k=>$v){
                if(!empty($v['photo'])){
                    $list[$k]['photo']=upload_file(tag_photo($v['photo']));
                }
                if(!empty($v['timeline'])){
                    $list[$k]['timline']=date('Y-m-d',$v['timeline']);
                }
            }
        }
        $vdata['list']=$list;
        $this->rdata['ReturnCode']   = '200';
        $this->rdata['ReturnInfo'] = '信息获取成功';
        $this->rdata['Content2'] = $vdata;
        $this->rdata['Content'] = $vdata;
        $this->response($this->rdata);
    }



    public function testSend_get(){
        $content='<table style="padding-left: 35%"><tr><td>用户名:</td><td>唐舞桐</td></tr><tr><td>手机号码:</td><td>15926290378</td></tr><tr><td>反馈内容:</td><td>c测试</td></tr></table>';
        $me=$this->send('1181452567@qq.com','海南智造慧科技有限公司','意见反馈 ',$content);
    }
    public function send($to, $username, $title, $content)
    {
        $this->load->library('mailer');
        //加载发送SMTP服务器配置
        $config = $this->db->select('value')->where(array('category' => 'email'))->order_by('id asc')->get('configs')->result_array();
        $obj = new Mailer();
        $obj->mail->Host = $config[0]['value'];
        $obj->mail->Port = $config[3]['value'];
        $obj->mail->Username = $config[1]['value'];
        $obj->mail->Password = $config[2]['value'];
        $title = $config[4]['value'];
        $res = $obj->sendmail(
            $to,
            $username,
            $title,
            $content
        );
        return $res;
    }
    public function imgResize($filename)
    {
        //获取图片水印文件
        $this->load->library('image_lib');
        //先调整图片的大小吧~
        $resize['image_library']='gd2';
        $resize['source_image'] = UPLOAD_PATH . $filename;
        $resize['maintain_ratio'] = TRUE;
        $resize['width'] = 480;
        $resize['height'] = 320 ;
        $resize['master_dim'] = 'width';
        $config['quality'] = '80%';
        $this->image_lib->initialize($resize);
        $this->image_lib->resize();
    }
    private function em_getallheaders($key=false) {

        foreach ($_SERVER as $name => $value) {

            if (substr($name, 0, 5) == 'HTTP_') {

                $headers[str_replace(' ', '-', ucwords(strtoupper(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        if ($key && isset($headers[$key])) {

            return $headers[$key];
        }
        return false;
    }
}